package com.ts.view;

import com.rio.core.L;
import com.rio.core.U;
import com.rio.helper.Sleeper;
import com.rio.layout.TaskManager;
import com.rio.layout.view.SimpleTask;
import com.ts.host.Event;
import com.ts.host.T;

import de.greenrobot.event.EventBus;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.TextView;

public class CountDownTextView extends TextView {

	
	private static int count;
	
	private static SimpleTask mTask;

	private SimpleTask getTask() {
		if (U.isNull(mTask)) {
			mTask = new SimpleTask() {

				@Override
				public Object onBGThread(Object... params) throws Exception {
					Sleeper.sleep(1000);
					return super.onBGThread(params);
				}
				
				@Override
				public void onUIThread(Object data, Object... params)
						throws Exception {
					//L.i();
					if(count > 1){
						count--;
						setText(count+"");
						TaskManager.getInstance().async(getTask());
					}else{
						EventBus.getDefault().post(new Event.ShowAction());
					}
					super.onUIThread(data, params);
				}

			};
		}
		return mTask;
	}
		

	
	public CountDownTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public void destory(){
		mTask = null;
	}
	
	public void start(){
		count = 20;
		setText(count+"");
		TaskManager.getInstance().async(getTask());
	}
}
